package com.gx303.wximageselect.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gx303.wximageselect.datas;
import com.gx303.wximageselect.Inter.ImageGridSelectInterface;
import com.gx303.wximageselect.Inter.ImageSelectInterface;
import com.gx303.wximageselect.adapter.ImageRecycleAdapter.ViewHolder;
import com.gx303.wximageselect.bean.Image;
import com.gx303.wxselectlibrary.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



/**
 * 图片Adapter
 * Created by Nereo on 2015/4/7.
 */
public class ImageGridAdapter extends  RecyclerView.Adapter  {
	private Context mContext;
	private ArrayList<Image> images ;
	private int viewHeight;
	ImageGridSelectInterface interface1;
	public ImageGridAdapter( Context context1 ,ArrayList<Image> images1,ImageGridSelectInterface it1)
	{
		
		images=images1;
		mContext=context1;
		interface1=it1;
		selectImages=new ArrayList<Image>();
	}

	public void setHeight(int height)
	{
		viewHeight=height;
	}
	@Override
	public int getItemCount() {
		// TODO Auto-generated method stub
		return images.size();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
		// TODO Auto-generated method stub
		View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_wx_image_select_grid_image, viewGroup, false);
        return new ViewHolder(v);
	}
	
	// 重写的自定义ViewHolder
    public static class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder
    {
        public ImageView mImageView;
        public ImageView mImageSelect;
        public LinearLayout ly_topright;
        public ViewHolder( View v )
        {
            super(v);
            mImageView = (ImageView) v.findViewById(R.id.imageView1);
            mImageSelect=(ImageView)v.findViewById(R.id.iv_select2);
            ly_topright=(LinearLayout)v.findViewById(R.id.ly_topright);
        }
    }

	@Override
	public void onBindViewHolder(
			android.support.v7.widget.RecyclerView.ViewHolder viewHolder, int arg1) {
			// TODO Auto-generated method stub
			Image im_now=images.get(arg1);
			File imageFile = new File(im_now.path);
			int picWidth=(viewHeight*im_now.width)/im_now.height;
			ImageView imageview=((ViewHolder)viewHolder).mImageView;
			ImageView iv_select=((ViewHolder)viewHolder).mImageSelect;
			LinearLayout ly_topright=((ViewHolder)viewHolder).ly_topright;
			// 显示图片
			Picasso.with(mContext)
                   .load(imageFile)
                   .placeholder(R.drawable.default_error)
                           //.error(R.drawable.default_error)
                   .resize(picWidth, viewHeight)
                   .centerCrop()
                   .into(imageview);
			imageview.setTag(arg1);
			ly_topright.setTag(arg1);
			imageview.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// 在相册选择界面，其他区域是预览单图
					int index=Integer.parseInt(v.getTag().toString());
//					Image im1=images.get(index);
//					int index=Integer.parseInt(v.getTag().toString());
					interface1.preview(index);

				}
			});
			ly_topright.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//在相册选择界面，右上角是选择
					int index=Integer.parseInt(v.getTag().toString());
					Image im1=images.get(index);
					if(!SelectImageExists(im1)&&SelectImageNum()>=datas.MAX_SELECT_IMAGE_NUM)
					{
						//当等于这个个数时，不可以再选
						Toast.makeText(mContext,mContext.getResources().getString(R.string.wx_image_select_msg_amount_limit), Toast.LENGTH_SHORT).show();
					}
					else
					{
						//小于的时候可以选
						SelectImage(im1);
						notifyDataSetChanged();
						interface1.clickTopRIght(im1);
					}
				}
			});
        setImageSelect(iv_select,selectImages.contains(im_now));
       
	}
	public void setImageSelect(ImageView iv1,boolean isselect)
	{
		if(isselect)
		{
			//有选择
			iv1.setImageDrawable(mContext.getResources().getDrawable(R.drawable.wx_image_select_select));
		}
		else
		{
			iv1.setImageDrawable(mContext.getResources().getDrawable(R.drawable.wx_image_select_unselect));
		}
	}
	
	public  ArrayList<Image> selectImages=new ArrayList<Image>();
	public void setSelectImage( ArrayList<Image> s1)
	{
		selectImages=s1;
	}
	public  void SelectImage(Image i1)
	{
		if(selectImages.contains(i1))
		{
			selectImages.remove(i1);
		}
		else
		{
			selectImages.add(i1);
		}
	}
	public  boolean SelectImageExists(Image i1)
	{
		return selectImages.contains(i1);
	}
	public  int SelectImageNum()
	{
		return selectImages.size();
	}
	public  ArrayList<Image> getSelectImage()
	{
		return selectImages;
	}
}
